package pl.edu.pw.elka.tinyrepo.client.view;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * @author Anna Stępień
 * @version 13-12-2011
 *
 */
public class GateAddressDialog
{
	private JTextField gateAddress;
	private JTextField gatePort;
	private JPanel inputPanel;
	
	private String address;
	private int port;
	
	public GateAddressDialog()
	{
		gateAddress = new JTextField(10);
		gatePort = new JTextField(10);
		inputPanel = new JPanel();
		inputPanel.add(new JLabel("Gate address:"));
	    inputPanel.add(gateAddress);
	    inputPanel.add(Box.createHorizontalStrut(15)); 
	    inputPanel.add(new JLabel("Gate port:"));
	    inputPanel.add(gatePort);
	}
	
	public void getInput()
	{
		while(true)
		{
			int result = JOptionPane.showConfirmDialog(null, inputPanel, 
		               "Gate address: ", JOptionPane.OK_CANCEL_OPTION);
			if (result == JOptionPane.OK_OPTION)
			{
				try
				{
					port = Integer.parseInt(gatePort.getText());
					if (!gateAddress.getText().isEmpty())
					{
						address = gateAddress.getText();
						break;
					}
				}
				catch(NumberFormatException e)
				{
					System.out.println("Wrong number format");
				}
			}
			else
			{
				System.exit(-1);
			}
		}

	}
	
	public String getGateAddress()
	{
		return address;
	}
	
	public int getGatePort()
	{
		return port;
	}
}
